/*
 * edge.java
 *
 * Created on March 11, 2011, 2:53 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ifs.RnD.Graph;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 *
 * @author anaklk
 */
public class Edge extends Component {

    /**
     * Starting vertex of the edge
     */
    public Vertex start;
    /**
     * Ending vertex of the edge
     */
    public Vertex end;
    String text = "";
    /**
     *
     */
    public Edge next;
    private Rectangle x1;
    private Rectangle x2;
    private boolean shift;
    private Color color = Color.GRAY;

    /** Creates a new instance of edge
     * @param s
     *  @param e
     */
    public Edge(Vertex s, Vertex e) {
        start = s;
        end = e;
        setBounds(0, 0, 5, 5);
    }

    /**
     * This method will draw the line according to the
     * @param g
     */
    @Override
    public void paint(Graphics g) {
        g.setColor(color);
        int a, b, c, d, sx = 0, sy = 0, z = 0;
        int x[] = new int[5];
        int y[] = new int[5];
        x1 = start.getBounds();
        x2 = end.getBounds();
        a = ((x1.x + 25) < (x2.x + 25)) ? (x1.x + 25) : (x2.x + 25);
        b = ((x1.y + 25) < x2.y) ? (x1.y + 25) : x2.y;
        c = (((x1.x + 25) < (x2.x + 25)) ? (x2.x + 25) - (x1.x + 25) : (x1.x + 25) - (x2.x + 25));
        d = ((x1.y + 25) < x2.y) ? x2.y - (x1.y + 25) : (x1.y + 25) - x2.y;

        if (start == end) {
            setBounds(a, b, 150, 150);
            g.drawOval(5, 25, 40, 80);
            g.drawString(text, 0, 100);
            return;
        }

        setBounds(a, b, c + 50, d + 50);
        if (!shift) {
            g.drawString(text, (c + 50) / 2, (d + 50) / 2);
        }
        if (a == x1.x + 25 && b == x1.y + 25) {
            a = 25;
            c = c + 25;
            b = 25;
            d = d + 25;
            sx = -10;
            sy = 10;
        } else if (a != x1.x + 25 && b == x1.y + 25) {
            a = c + 25;
            c = 25;
            b = 25;
            d = d + 25;
            sx = -10;
            sy = -10;
        } else if (a == x1.x + 25 && b != x1.y + 25) {
            a = 25;
            c = c + 25;
            b = d + 25;
            d = 25;
            sx = 10;
            sy = 10;
        } else {
            a = c + 25;
            c = 25;
            b = d + 25;
            d = 25;
            sx = 10;
            sy = -10;
        }
        if (shift) {
        } else {
            g.drawLine(a, b, c, d);
        }

        a = a - c;
        b = b - d;
        z = (int) Math.sqrt((a) * (a) + (b) * (b));
        x[0] = x[4] = (25) * (a) / z + c;
        y[0] = y[4] = (25) * (b) / z + d;
        x[2] = (30) * (a) / z + c;
        y[2] = (30) * (b) / z + d;
        c = y[2] - y[0];
        d = x[2] - x[0];
        x[1] = x[2] + c;
        x[3] = x[2] - c;
        y[1] = y[2] - d;
        y[3] = y[2] + d;
    }

    /**
     * when edge shift
     */
    public void shift() {
        shift = true;
    }

    /**
     *when edge unshift
     */
    public void unshift() {
        shift = false;
    }

    /**
     * This will set the color of the Edge
     * @param c
     */
    public void setColor(Color c) {
        color = c;
    }

    /**
     * This method will print the starting position and end position of the Edge
     * @return string of position values
     */
    @Override
    public String toString() {
        return "<" + start + "," + end + ">";
    }
}
